from functools import wraps
from flask import jsonify

def run_safe(_logger):
    def decorator(func):
        def decorated_function(*args,**kwargs):
            try:
                return func(*args,**kwargs)
            except:
                _logger.error(traceback.format_exc())
        return decorated_function 
    return decorator

def json_safe():
    def decorator(func):
        def decorated_function(*args,**kwargs):
            result = {"success":True,"msg":"","error":0};
            try:
                data=func(*args,**kwargs)
                if data is not None:
                    result['data']=data
                    #TODO:extend the result
            except Exception,e:
                print e.message 
                result["error"] = 1
                result["success"] = False
                result["msg"] = e.message
                #print "Got error! ", repr(e)
            return jsonify(result)
        return decorated_function 
    return decorator
